<!DOCTYPE html>
<html lang="zh_CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="generator" content="VuePress 2.0.0-beta.45">
    <style>
      :root {
        --c-bg: #fff;
      }
      html.dark {
        --c-bg: #22272e;
      }
      html, body {
        background-color: var(--c-bg);
      }
    </style>
    <script>
      const userMode = localStorage.getItem('vuepress-color-scheme');
			const systemDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
			if (userMode === 'dark' || (userMode !== 'light' && systemDarkMode)) {
				document.documentElement.classList.toggle('dark', true);
			}
    </script>
    <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico"><title>Cobra 中文文档 | 科科人神</title><meta name="description" content="科科人神，后端软件工程师，喜欢云原生">
    <link rel="modulepreload" href="/assets/app.80e5dc69.js"><link rel="modulepreload" href="/assets/index.html.9d9ba895.js"><link rel="modulepreload" href="/assets/index.html.467e6eba.js"><link rel="prefetch" href="/assets/index.html.cba988ab.js"><link rel="prefetch" href="/assets/1.html.8ac90e92.js"><link rel="prefetch" href="/assets/19.html.4444be95.js"><link rel="prefetch" href="/assets/必备知识.html.7da85c42.js"><link rel="prefetch" href="/assets/必备知识罗列.html.5e18db77.js"><link rel="prefetch" href="/assets/1.html.356f0755.js"><link rel="prefetch" href="/assets/2.html.085440fc.js"><link rel="prefetch" href="/assets/3.html.09effb5c.js"><link rel="prefetch" href="/assets/1.html.50fb5135.js"><link rel="prefetch" href="/assets/1.html.17222973.js"><link rel="prefetch" href="/assets/1.html.0a197ef1.js"><link rel="prefetch" href="/assets/4-1.html.073fbd99.js"><link rel="prefetch" href="/assets/4-2.html.64039343.js"><link rel="prefetch" href="/assets/4.html.021c0881.js"><link rel="prefetch" href="/assets/5-6-1.html.34978ed1.js"><link rel="prefetch" href="/assets/5-6-2.html.f004f560.js"><link rel="prefetch" href="/assets/5-6.html.f5ad3835.js"><link rel="prefetch" href="/assets/index.html.f3622043.js"><link rel="prefetch" href="/assets/index.html.a2792a89.js"><link rel="prefetch" href="/assets/index.html.5d63961d.js"><link rel="prefetch" href="/assets/index.html.0b5fc023.js"><link rel="prefetch" href="/assets/index.html.2f735cf1.js"><link rel="prefetch" href="/assets/index.html.9d71a6d6.js"><link rel="prefetch" href="/assets/bash_completions.html.ed68147e.js"><link rel="prefetch" href="/assets/man_docs.html.2f0dd0c9.js"><link rel="prefetch" href="/assets/md_docs.html.20de452a.js"><link rel="prefetch" href="/assets/rest_docs.html.b77e8a2e.js"><link rel="prefetch" href="/assets/zsh_completions.html.60976ab2.js"><link rel="prefetch" href="/assets/syslog.html.48554683.js"><link rel="prefetch" href="/assets/daily_dep.html.2f155f5c.js"><link rel="prefetch" href="/assets/getting_started.html.769a2eaa.js"><link rel="prefetch" href="/assets/installation.html.e6b0002b.js"><link rel="prefetch" href="/assets/migrating.html.128871c1.js"><link rel="prefetch" href="/assets/new_project.html.8326d390.js"><link rel="prefetch" href="/assets/uninstalling.html.26650652.js"><link rel="prefetch" href="/assets/deduction.html.d4acd3fa.js"><link rel="prefetch" href="/assets/ensure_mechanics.html.603c62de.js"><link rel="prefetch" href="/assets/404.html.a15b4cbd.js"><link rel="prefetch" href="/assets/index.html.29aac47c.js"><link rel="prefetch" href="/assets/index.html.b64fbb23.js"><link rel="prefetch" href="/assets/index.html.cd48e230.js"><link rel="prefetch" href="/assets/index.html.6b048e76.js"><link rel="prefetch" href="/assets/index.html.7f0a6e54.js"><link rel="prefetch" href="/assets/index.html.b5a71586.js"><link rel="prefetch" href="/assets/index.html.60805430.js"><link rel="prefetch" href="/assets/index.html.60736b0b.js"><link rel="prefetch" href="/assets/index.html.80dddd6d.js"><link rel="prefetch" href="/assets/index.html.cc3c6a0d.js"><link rel="prefetch" href="/assets/index.html.44e62fda.js"><link rel="prefetch" href="/assets/index.html.015a0b2c.js"><link rel="prefetch" href="/assets/index.html.415d4517.js"><link rel="prefetch" href="/assets/index.html.d1d65e23.js"><link rel="prefetch" href="/assets/index.html.725ac526.js"><link rel="prefetch" href="/assets/index.html.c824ee30.js"><link rel="prefetch" href="/assets/1.html.4fb25663.js"><link rel="prefetch" href="/assets/19.html.ded8a9c9.js"><link rel="prefetch" href="/assets/必备知识.html.0ff3a77d.js"><link rel="prefetch" href="/assets/必备知识罗列.html.85e0f635.js"><link rel="prefetch" href="/assets/1.html.4f76037b.js"><link rel="prefetch" href="/assets/2.html.05a6e91e.js"><link rel="prefetch" href="/assets/3.html.30d4c4b6.js"><link rel="prefetch" href="/assets/1.html.ce86a757.js"><link rel="prefetch" href="/assets/1.html.0f8a9434.js"><link rel="prefetch" href="/assets/1.html.f4ee2fe7.js"><link rel="prefetch" href="/assets/4-1.html.c7cc75b6.js"><link rel="prefetch" href="/assets/4-2.html.36807608.js"><link rel="prefetch" href="/assets/4.html.c03ae622.js"><link rel="prefetch" href="/assets/5-6-1.html.651e5517.js"><link rel="prefetch" href="/assets/5-6-2.html.5ccfdb5e.js"><link rel="prefetch" href="/assets/5-6.html.882bc951.js"><link rel="prefetch" href="/assets/index.html.3da373d1.js"><link rel="prefetch" href="/assets/index.html.6ec0b380.js"><link rel="prefetch" href="/assets/index.html.f8c39be7.js"><link rel="prefetch" href="/assets/index.html.e8d4dc57.js"><link rel="prefetch" href="/assets/index.html.2275c56f.js"><link rel="prefetch" href="/assets/index.html.711c721b.js"><link rel="prefetch" href="/assets/bash_completions.html.31fa6426.js"><link rel="prefetch" href="/assets/man_docs.html.cc0f68c6.js"><link rel="prefetch" href="/assets/md_docs.html.dd10674f.js"><link rel="prefetch" href="/assets/rest_docs.html.46da86e5.js"><link rel="prefetch" href="/assets/zsh_completions.html.64d57b81.js"><link rel="prefetch" href="/assets/syslog.html.5324d692.js"><link rel="prefetch" href="/assets/daily_dep.html.b7a8b70b.js"><link rel="prefetch" href="/assets/getting_started.html.29c6534a.js"><link rel="prefetch" href="/assets/installation.html.b11da95a.js"><link rel="prefetch" href="/assets/migrating.html.92825adc.js"><link rel="prefetch" href="/assets/new_project.html.8fcfe511.js"><link rel="prefetch" href="/assets/uninstalling.html.34c1a6fb.js"><link rel="prefetch" href="/assets/deduction.html.2f0a618b.js"><link rel="prefetch" href="/assets/ensure_mechanics.html.7818d8b5.js"><link rel="prefetch" href="/assets/404.html.37719150.js"><link rel="prefetch" href="/assets/index.html.bc2fbcdd.js"><link rel="prefetch" href="/assets/index.html.2363e42b.js"><link rel="prefetch" href="/assets/index.html.691f6b43.js"><link rel="prefetch" href="/assets/index.html.c73599f4.js"><link rel="prefetch" href="/assets/index.html.32559aa6.js"><link rel="prefetch" href="/assets/index.html.283a60cc.js"><link rel="prefetch" href="/assets/index.html.b72bddc9.js"><link rel="prefetch" href="/assets/index.html.aebed871.js"><link rel="prefetch" href="/assets/index.html.1bace120.js"><link rel="prefetch" href="/assets/index.html.7913d44a.js"><link rel="prefetch" href="/assets/index.html.4e5360d4.js"><link rel="prefetch" href="/assets/index.html.028748b8.js"><link rel="prefetch" href="/assets/index.html.e9cb3a86.js"><link rel="prefetch" href="/assets/index.html.e31e8159.js"><link rel="prefetch" href="/assets/index.html.01049197.js"><link rel="prefetch" href="/assets/404.30cc8863.js"><link rel="prefetch" href="/assets/HomePage.4e823a0b.js"><link rel="prefetch" href="/assets/Layout.61f5e0fd.js"><link rel="prefetch" href="/assets/Links.8680cda6.js"><link rel="prefetch" href="/assets/Post.93b09438.js"><link rel="prefetch" href="/assets/Tags.3d9bb337.js">
    <link rel="stylesheet" href="/assets/style.8b24233a.css">
  </head>
  <body>
    <div id="app"><!--[--><div class="theme-container"><!--[--><header class="navbar is-fixed is-visible invert"><span><a href="/" class=""><span class="site-name">$ cd /home/</span></a></span><div class="navbar-items-wrapper" style=""><!--[--><!--]--><nav class="navbar-items can-hide"><!--[--><div class="navbar-item"><a href="/" class="" aria-label="home"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="0 0 20 20" fill="currentColor"><path d="M10.707 2.293a1 1 0 00-1.414 0l-7 7a1 1 0 001.414 1.414L4 10.414V17a1 1 0 001 1h2a1 1 0 001-1v-2a1 1 0 011-1h2a1 1 0 011 1v2a1 1 0 001 1h2a1 1 0 001-1v-6.586l.293.293a1 1 0 001.414-1.414l-7-7z"/></svg></span><span>home</span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/tags/" class="" aria-label="归档"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-1.6 -1.6 19.2 19.2" fill="currentColor"><path d="M2 2a1 1 0 011-1h4.586a1 1 0 01.707.293l7 7a1 1 0 010 1.414l-4.586 4.586a1 1 0 01-1.414 0l-7-7A1 1 0 012 6.586V2zm3.5 4a1.5 1.5 0 100-3 1.5 1.5 0 000 3z"/><path d="M1.293 7.793A1 1 0 011 7.086V2a1 1 0 00-1 1v4.586a1 1 0 00.293.707l7 7a1 1 0 001.414 0l.043-.043-7.457-7.457z"/></svg></span><span>归档</span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/links/" class="" aria-label="作品合集"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-79.52 -43.52 599.04 599.04" fill="currentColor"><path d="M382.7 292.5l2.7 2.7-170-167.3c-3.5-3.5-9.7-3.7-13.8-.5L144.3 171c-4.2 3.2-4.6 8.7-1.1 12.2l68.1 64.3c3.6 3.5 9.9 3.7 14 .5l.1-.1c4.1-3.2 10.4-3 14 .5l84 81.3c3.6 3.5 3.2 9-.9 12.2l-93.2 74c-4.2 3.3-10.5 3.1-14.2-.4L63.2 268c-3.5-3.5-9.7-3.7-13.9-.5L3.5 302.4c-4.2 3.2-4.7 8.7-1.2 12.2L211 510.7s7.4 6.8 17.3-.8l198-163.9c4-3.2 4.4-8.7.7-12.2zm54.5-83.4L226.7 2.5c-1.5-1.2-8-5.5-16.3 1.1L3.6 165.7c-4.2 3.2-4.8 8.7-1.2 12.2l42.3 41.7 171.7 165.1c3.7 3.5 10.1 3.7 14.3.4l50.2-38.8-.3-.3 7.7-6c4.2-3.2 4.6-8.7.9-12.2l-57.1-54.4c-3.6-3.5-10-3.7-14.2-.5l-.1.1c-4.2 3.2-10.5 3.1-14.2-.4L109 180.8c-3.6-3.5-3.1-8.9 1.1-12.2l92.2-71.5c4.1-3.2 10.3-3 13.9.5l160.4 159c3.7 3.5 10 3.7 14.1.5l45.8-35.8c4.1-3.2 4.4-8.7.7-12.2z"/></svg></span><span>作品合集</span><!--[--><!--]--></a></div><!--]--><div class="navbar-item"><a style="cursor:pointer;"><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="0 0 24 24" fill="currentColor"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 2c4.968 0 9 4.032 9 9s-4.032 9-9 9-9-4.032-9-9 4.032-9 9-9zm0 16c3.867 0 7-3.133 7-7 0-3.868-3.133-7-7-7-3.868 0-7 3.132-7 7 0 3.867 3.132 7 7 7zm8.485.071l2.829 2.828-1.415 1.415-2.828-2.829 1.414-1.414z"/></svg></span><span>搜索</span></a></div></nav><!--[--><!--]--><!----></div></header><!--]--><div class="sidebar-mask"></div><!--[--><aside class="sidebar"><nav class="navbar-items"><!--[--><div class="navbar-item"><a href="/" class="" aria-label="home"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="0 0 20 20" fill="currentColor"><path d="M10.707 2.293a1 1 0 00-1.414 0l-7 7a1 1 0 001.414 1.414L4 10.414V17a1 1 0 001 1h2a1 1 0 001-1v-2a1 1 0 011-1h2a1 1 0 011 1v2a1 1 0 001 1h2a1 1 0 001-1v-6.586l.293.293a1 1 0 001.414-1.414l-7-7z"/></svg></span><span>home</span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/tags/" class="" aria-label="归档"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-1.6 -1.6 19.2 19.2" fill="currentColor"><path d="M2 2a1 1 0 011-1h4.586a1 1 0 01.707.293l7 7a1 1 0 010 1.414l-4.586 4.586a1 1 0 01-1.414 0l-7-7A1 1 0 012 6.586V2zm3.5 4a1.5 1.5 0 100-3 1.5 1.5 0 000 3z"/><path d="M1.293 7.793A1 1 0 011 7.086V2a1 1 0 00-1 1v4.586a1 1 0 00.293.707l7 7a1 1 0 001.414 0l.043-.043-7.457-7.457z"/></svg></span><span>归档</span><!--[--><!--]--></a></div><div class="navbar-item"><a href="/links/" class="" aria-label="作品合集"><!--[--><!--]--><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-79.52 -43.52 599.04 599.04" fill="currentColor"><path d="M382.7 292.5l2.7 2.7-170-167.3c-3.5-3.5-9.7-3.7-13.8-.5L144.3 171c-4.2 3.2-4.6 8.7-1.1 12.2l68.1 64.3c3.6 3.5 9.9 3.7 14 .5l.1-.1c4.1-3.2 10.4-3 14 .5l84 81.3c3.6 3.5 3.2 9-.9 12.2l-93.2 74c-4.2 3.3-10.5 3.1-14.2-.4L63.2 268c-3.5-3.5-9.7-3.7-13.9-.5L3.5 302.4c-4.2 3.2-4.7 8.7-1.2 12.2L211 510.7s7.4 6.8 17.3-.8l198-163.9c4-3.2 4.4-8.7.7-12.2zm54.5-83.4L226.7 2.5c-1.5-1.2-8-5.5-16.3 1.1L3.6 165.7c-4.2 3.2-4.8 8.7-1.2 12.2l42.3 41.7 171.7 165.1c3.7 3.5 10.1 3.7 14.3.4l50.2-38.8-.3-.3 7.7-6c4.2-3.2 4.6-8.7.9-12.2l-57.1-54.4c-3.6-3.5-10-3.7-14.2-.5l-.1.1c-4.2 3.2-10.5 3.1-14.2-.4L109 180.8c-3.6-3.5-3.1-8.9 1.1-12.2l92.2-71.5c4.1-3.2 10.3-3 13.9.5l160.4 159c3.7 3.5 10 3.7 14.1.5l45.8-35.8c4.1-3.2 4.4-8.7.7-12.2z"/></svg></span><span>作品合集</span><!--[--><!--]--></a></div><!--]--><div class="navbar-item"><a style="cursor:pointer;"><span class="nav-icon"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="0 0 24 24" fill="currentColor"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 2c4.968 0 9 4.032 9 9s-4.032 9-9 9-9-4.032-9-9 4.032-9 9-9zm0 16c3.867 0 7-3.133 7-7 0-3.868-3.133-7-7-7-3.868 0-7 3.132-7 7 0 3.867 3.132 7 7 7zm8.485.071l2.829 2.828-1.415 1.415-2.828-2.829 1.414-1.414z"/></svg></span><span>搜索</span></a></div></nav><!--[--><!--]--><ul class="sidebar-items"><!--[--><li><p tabindex="0" class="sidebar-item sidebar-heading">Cobra 中文文档 <!----></p><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#commands" class="router-link-active router-link-exact-active sidebar-item" aria-label="Commands"><!--[--><!--]--><!----><span>Commands</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#flags" class="router-link-active router-link-exact-active sidebar-item" aria-label="Flags"><!--[--><!--]--><!----><span>Flags</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#using-the-cobra-generator" class="router-link-active router-link-exact-active sidebar-item" aria-label="Using the Cobra Generator"><!--[--><!--]--><!----><span>Using the Cobra Generator</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#using-the-cobra-library" class="router-link-active router-link-exact-active sidebar-item" aria-label="Using the Cobra Library"><!--[--><!--]--><!----><span>Using the Cobra Library</span><!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#create-rootcmd" class="router-link-active router-link-exact-active sidebar-item" aria-label="Create rootCmd"><!--[--><!--]--><!----><span>Create rootCmd</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#create-your-main-go" class="router-link-active router-link-exact-active sidebar-item" aria-label="Create your main.go"><!--[--><!--]--><!----><span>Create your main.go</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#create-additional-commands" class="router-link-active router-link-exact-active sidebar-item" aria-label="Create additional commands"><!--[--><!--]--><!----><span>Create additional commands</span><!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#working-with-flags" class="router-link-active router-link-exact-active sidebar-item" aria-label="Working with Flags"><!--[--><!--]--><!----><span>Working with Flags</span><!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#assign-flags-to-a-command" class="router-link-active router-link-exact-active sidebar-item" aria-label="Assign flags to a command"><!--[--><!--]--><!----><span>Assign flags to a command</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#persistent-flags" class="router-link-active router-link-exact-active sidebar-item" aria-label="Persistent Flags"><!--[--><!--]--><!----><span>Persistent Flags</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#local-flags" class="router-link-active router-link-exact-active sidebar-item" aria-label="Local Flags"><!--[--><!--]--><!----><span>Local Flags</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#local-flag-on-parent-commands" class="router-link-active router-link-exact-active sidebar-item" aria-label="Local Flag on Parent Commands"><!--[--><!--]--><!----><span>Local Flag on Parent Commands</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#bind-flags-with-config" class="router-link-active router-link-exact-active sidebar-item" aria-label="Bind Flags with Config"><!--[--><!--]--><!----><span>Bind Flags with Config</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#required-flags" class="router-link-active router-link-exact-active sidebar-item" aria-label="Required flags"><!--[--><!--]--><!----><span>Required flags</span><!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#positional-and-custom-arguments" class="router-link-active router-link-exact-active sidebar-item" aria-label="Positional and Custom Arguments"><!--[--><!--]--><!----><span>Positional and Custom Arguments</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#example" class="router-link-active router-link-exact-active sidebar-item" aria-label="Example"><!--[--><!--]--><!----><span>Example</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#help-command" class="router-link-active router-link-exact-active sidebar-item" aria-label="Help Command"><!--[--><!--]--><!----><span>Help Command</span><!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#example-1" class="router-link-active router-link-exact-active sidebar-item" aria-label="Example"><!--[--><!--]--><!----><span>Example</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#defining-your-own-help" class="router-link-active router-link-exact-active sidebar-item" aria-label="Defining your own help"><!--[--><!--]--><!----><span>Defining your own help</span><!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#usage-message" class="router-link-active router-link-exact-active sidebar-item" aria-label="Usage Message"><!--[--><!--]--><!----><span>Usage Message</span><!--[--><!--]--></a><ul style="" class="sidebar-item-children"><!--[--><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#example-2" class="router-link-active router-link-exact-active sidebar-item" aria-label="Example"><!--[--><!--]--><!----><span>Example</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#defining-your-own-usage" class="router-link-active router-link-exact-active sidebar-item" aria-label="Defining your own usage"><!--[--><!--]--><!----><span>Defining your own usage</span><!--[--><!--]--></a><!----></li><!--]--></ul></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#version-flag" class="router-link-active router-link-exact-active sidebar-item" aria-label="Version Flag"><!--[--><!--]--><!----><span>Version Flag</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#prerun-and-postrun-hooks" class="router-link-active router-link-exact-active sidebar-item" aria-label="PreRun and PostRun Hooks"><!--[--><!--]--><!----><span>PreRun and PostRun Hooks</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#suggestions-when-unknown-command-happens" class="router-link-active router-link-exact-active sidebar-item" aria-label="Suggestions when &quot;unknown command&quot; happens"><!--[--><!--]--><!----><span>Suggestions when &quot;unknown command&quot; happens</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#generating-documentation-for-your-command" class="router-link-active router-link-exact-active sidebar-item" aria-label="Generating documentation for your command"><!--[--><!--]--><!----><span>Generating documentation for your command</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#generating-bash-completions" class="router-link-active router-link-exact-active sidebar-item" aria-label="Generating bash completions"><!--[--><!--]--><!----><span>Generating bash completions</span><!--[--><!--]--></a><!----></li><li><a aria-current="page" href="/posts/2023/4/docs/Cobra/#generating-zsh-completions" class="router-link-active router-link-exact-active sidebar-item" aria-label="Generating zsh completions"><!--[--><!--]--><!----><span>Generating zsh completions</span><!--[--><!--]--></a><!----></li><!--]--></ul></li><!--]--></ul><!--[--><!--]--></aside><!--]--><div class="page-content"><!--[--><main class="page"><!--[--><div class="article-header" style=""><!----><div class="article-header-content"><!----><h1 class="article-title"></h1><!----><div class="article-icons"><div class="article-icon"><svg class="ov-icon" style="font-size:1.08em;" aria-hidden="true" width="17.28" height="17.28" viewbox="-75.52 -43.52 599.04 599.04" fill="currentColor"><path d="M313.6 304c-28.7 0-42.5 16-89.6 16-47.1 0-60.8-16-89.6-16C60.2 304 0 364.2 0 438.4V464c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48v-25.6c0-74.2-60.2-134.4-134.4-134.4zM400 464H48v-25.6c0-47.6 38.8-86.4 86.4-86.4 14.6 0 38.3 16 89.6 16 51.7 0 74.9-16 89.6-16 47.6 0 86.4 38.8 86.4 86.4V464zM224 288c79.5 0 144-64.5 144-144S303.5 0 224 0 80 64.5 80 144s64.5 144 144 144zm0-240c52.9 0 96 43.1 96 96s-43.1 96-96 96-96-43.1-96-96 43.1-96 96-96z"/></svg><span>科科人神</span></div><!----><div class="article-icon"><svg class="ov-icon" style="font-size:1.08em;" aria-hidden="true" width="17.28" height="17.28" viewbox="0 0 24 24" fill="currentColor"><path fill="none" d="M0 0h24v24H0z"/><path d="M17.618 5.968l1.453-1.453 1.414 1.414-1.453 1.453a9 9 0 11-1.414-1.414zM12 20a7 7 0 100-14 7 7 0 000 14zM11 8h2v6h-2V8zM8 1h8v2H8V1z"/></svg><span>20 min</span></div></div></div><!----></div><!--[--><!--]--><!--]--><div class="theme-gungnir-content"><!--[--><!--]--><!--[--><h1 id="cobra-中文文档" tabindex="-1"><a class="header-anchor" href="#cobra-中文文档" aria-hidden="true">#</a> Cobra 中文文档</h1><p>Cobra 中文文档，本文档基于 <a href="https://github.com/spf13/cobra" target="_blank" rel="noopener noreferrer">cobra</a> 官方文档。不定期更新。</p><p><img src="https://cloud.githubusercontent.com/assets/173412/10886352/ad566232-814f-11e5-9cd0-aa101788c117.png" alt="cobra logo"></p><p>Cobra 既是一个创建强大的现代 CLI 应用程序的库，也是一个生成应用和命令文件的程序。</p><p>许多最广泛使用的 Go 项目都是使用 Cobra 构建的，包括:</p><ul><li><a href="http://kubernetes.io/" target="_blank" rel="noopener noreferrer">Kubernetes</a></li><li><a href="http://gohugo.io" target="_blank" rel="noopener noreferrer">Hugo</a></li><li><a href="https://github.com/coreos/rkt" target="_blank" rel="noopener noreferrer">rkt</a></li><li><a href="https://github.com/coreos/etcd" target="_blank" rel="noopener noreferrer">etcd</a></li><li><a href="https://github.com/moby/moby" target="_blank" rel="noopener noreferrer">Moby (former Docker)</a></li><li><a href="https://github.com/docker/distribution" target="_blank" rel="noopener noreferrer">Docker (distribution)</a></li><li><a href="https://www.openshift.com/" target="_blank" rel="noopener noreferrer">OpenShift</a></li><li><a href="https://github.com/derekparker/delve" target="_blank" rel="noopener noreferrer">Delve</a></li><li><a href="http://www.gopherjs.org/" target="_blank" rel="noopener noreferrer">GopherJS</a></li><li><a href="http://www.cockroachlabs.com/" target="_blank" rel="noopener noreferrer">CockroachDB</a></li><li><a href="http://www.blevesearch.com/" target="_blank" rel="noopener noreferrer">Bleve</a></li><li><a href="http://www.projectatomic.io/" target="_blank" rel="noopener noreferrer">ProjectAtomic (enterprise)</a></li><li><a href="https://github.com/giantswarm/gsctl" target="_blank" rel="noopener noreferrer">Giant Swarm&#39;s gsctl</a></li><li><a href="https://github.com/nanobox-io/nanobox" target="_blank" rel="noopener noreferrer">Nanobox</a>/<a href="https://github.com/nanopack" target="_blank" rel="noopener noreferrer">Nanopack</a></li><li><a href="http://rclone.org/" target="_blank" rel="noopener noreferrer">rclone</a></li><li><a href="https://github.com/bogem/nehm" target="_blank" rel="noopener noreferrer">nehm</a></li><li><a href="https://github.com/alibaba/pouch" target="_blank" rel="noopener noreferrer">Pouch</a></li></ul><p><a href="https://travis-ci.org/spf13/cobra" target="_blank" rel="noopener noreferrer"><img src="https://travis-ci.org/spf13/cobra.svg" alt="Build Status" title="Travis CI status"></a><a href="https://circleci.com/gh/spf13/cobra" target="_blank" rel="noopener noreferrer"><img src="https://circleci.com/gh/spf13/cobra.png?circle-token=:circle-token" alt="CircleCI status" title="CircleCI status"></a><a href="https://godoc.org/github.com/spf13/cobra" target="_blank" rel="noopener noreferrer"><img src="https://godoc.org/github.com/spf13/cobra?status.svg" alt="GoDoc"></a></p><h1 id="table-of-contents" tabindex="-1"><a class="header-anchor" href="#table-of-contents" aria-hidden="true">#</a> Table of Contents</h1><ul><li><a href="#overview">Overview</a></li><li><a href="#concepts">Concepts</a><ul><li><a href="#commands">Commands</a></li><li><a href="#flags">Flags</a></li></ul></li><li><a href="#installing">Installing</a></li><li><a href="#getting-started">Getting Started</a><ul><li><a href="#using-the-cobra-generator">Using the Cobra Generator</a></li><li><a href="#using-the-cobra-library">Using the Cobra Library</a></li><li><a href="#working-with-flags">Working with Flags</a></li><li><a href="#positional-and-custom-arguments">Positional and Custom Arguments</a></li><li><a href="#example">Example</a></li><li><a href="#help-command">Help Command</a></li><li><a href="#usage-message">Usage Message</a></li><li><a href="#prerun-and-postrun-hooks">PreRun and PostRun Hooks</a></li><li><a href="#suggestions-when-unknown-command-happens">Suggestions when &quot;unknown command&quot; happens</a></li><li><a href="#generating-documentation-for-your-command">Generating documentation for your command</a></li><li><a href="#generating-bash-completions">Generating bash completions</a></li></ul></li><li><a href="#contributing">Contributing</a></li><li><a href="#license">License</a></li></ul><h1 id="overview" tabindex="-1"><a class="header-anchor" href="#overview" aria-hidden="true">#</a> Overview</h1><p>Cobra 是一个提供了简单的接口，可以创建类似 <code>git</code> 和 <code>go tools</code> 的强大的现代 CLI 接口的库。</p><p>Cobra 也是一个生成应用脚手架的应用程序，快速开发基于 Cobra 的应用程序。</p><p>Cobra 支持:</p><ul><li>基于子命令的简易 CLI: <code>app server</code>, <code>app fetch</code> 等。</li><li>完全符合 POSIX 标准</li><li>嵌套的子命令</li><li>全局，本地和级联标志</li><li><code>cobra init appname</code> 和 <code>cobra add cmdname</code> 命令轻松生成应用程序和命令</li><li>智能化建议 (<code>app srver</code>... did you mean <code>app server</code>?)</li><li>自动生成命令和标志的 <code>help</code></li><li>自动帮助标志识别 <code>-h</code>，<code>--help</code> 等。</li><li>为你的应用程序自动生成 bash autocomplete</li><li>为你的应用程序自动生成 man pages</li><li>命令别名，以便可以在不破坏它们的情况下进行更改</li><li>灵活的定义你自己的帮助，用法等。</li><li>可选择与 <a href="http://github.com/spf13/viper" target="_blank" rel="noopener noreferrer">viper</a>紧密集成，用于 12-factor 应用</li></ul><h1 id="concepts" tabindex="-1"><a class="header-anchor" href="#concepts" aria-hidden="true">#</a> Concepts</h1><p>Cobra 建立在 <strong>Commands</strong>，<strong>Args</strong> 和 <strong>Flags</strong> 的结构之上。</p><p><strong>Commands</strong> 代表动作，<strong>Args</strong> 代表事物，<strong>Flags</strong> 是这些动作的修饰符。</p><p>最好的应用程序在使用时会像句子一样读取。 用户会知道如何使用该应用程序，因为他们将原生地了解如何使用它。</p><p>要遵循的模式是 <code>APPNAME VERB NOUN --ADJECTIVE</code>。 或 <code>APPNAME COMMAND ARG --FLAG</code></p><p>一些好的现实的例子可以更好地说明这一点。</p><p>下面的例子，<code>server</code> 是一个命令，<code>port</code> 是一个标志:</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>hugo server --port<span class="token operator">=</span><span class="token number">1313</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>在这个命令中，我们告诉 Git 只克隆这个 url 地址。</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code><span class="token function">git</span> clone URL --bare
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h2 id="commands" tabindex="-1"><a class="header-anchor" href="#commands" aria-hidden="true">#</a> Commands</h2><p>命令是应用程序的中心点。应用程序支持的每个交互都将包含在命令中。命令可以有子命令并可选择运行一个动作。</p><p>在上面的例子中，<code>server</code> 就是一个命令。</p><p><a href="https://godoc.org/github.com/spf13/cobra#Command" target="_blank" rel="noopener noreferrer">更多关于 cobra.Command</a></p><h2 id="flags" tabindex="-1"><a class="header-anchor" href="#flags" aria-hidden="true">#</a> Flags</h2><p>标志是一种修改命令行为的方法。Cobra 完全符合 POSIX 标准以及 Go <a href="https://golang.org/pkg/flag/" target="_blank" rel="noopener noreferrer">flag package</a>。 Cobra 命令可以定义持久保存到子命令和标志的标志，这些命令和标志仅对该命令可用。</p><p>在上面的例子中，<code>port</code> 是一个标志。</p><p>标志功能由 <a href="https://github.com/spf13/pflag" target="_blank" rel="noopener noreferrer">pflag library</a> 提供，是 flag 库的一个分支，它在添加 POSIX 合规性时保持相同的接口。</p><h1 id="installing" tabindex="-1"><a class="header-anchor" href="#installing" aria-hidden="true">#</a> Installing</h1><p>Cobra 使用很简单，首先，使用 <code>go get</code> 安装最新的版本。此命令将安装 <code>cobra</code> 生成器可执行文件以及库及其依赖项：</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>go get -u github.com/spf13/cobra/cobra
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>接下来, 在你的应用引入 Cobra:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token string">&quot;github.com/spf13/cobra&quot;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h1 id="getting-started" tabindex="-1"><a class="header-anchor" href="#getting-started" aria-hidden="true">#</a> Getting Started</h1><p>虽然欢迎提供自己的组织, 但通常基于 Cobra 的应用程序将遵循以下组织结构:</p><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>  ▾ appName/
    ▾ cmd/
        add.go
        your.go
        commands.go
        here.go
      main.go
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在一个 Cobra 应用里, 通常这个 <code>main.go</code> 文件仅仅只有一个目的：初始化 Cobra。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
  <span class="token string">&quot;{pathToYourApp}/cmd&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  cmd<span class="token punctuation">.</span><span class="token function">Execute</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="using-the-cobra-generator" tabindex="-1"><a class="header-anchor" href="#using-the-cobra-generator" aria-hidden="true">#</a> Using the Cobra Generator</h2><p>Cobra 提供了自己的程序，可以创建你的应用程序并添加想要的任何命令。 这是将 Cobra 整合到你的应用程序中的最简单方法。</p><p><a href="https://github.com/spf13/cobra/blob/master/cobra/README.md" target="_blank" rel="noopener noreferrer">这里</a> 你可以找到更多相关信息。</p><h2 id="using-the-cobra-library" tabindex="-1"><a class="header-anchor" href="#using-the-cobra-library" aria-hidden="true">#</a> Using the Cobra Library</h2><p>要手动实现 Cobra，需要创建一个空的 <code>main.go</code> 文件和一个 <code>rootCmd</code> 文件。你可以选择根据需要提供其他命令。</p><h3 id="create-rootcmd" tabindex="-1"><a class="header-anchor" href="#create-rootcmd" aria-hidden="true">#</a> Create rootCmd</h3><p>Cobra 不需要任何特殊的构造函数。只需创建命令即可。</p><p>理想情况下，你将它放在 <code>app/cmd/root.go</code>:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">var</span> rootCmd <span class="token operator">=</span> <span class="token operator">&amp;</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">{</span>
  Use<span class="token punctuation">:</span>   <span class="token string">&quot;hugo&quot;</span><span class="token punctuation">,</span>
  Short<span class="token punctuation">:</span> <span class="token string">&quot;Hugo is a very fast static site generator&quot;</span><span class="token punctuation">,</span>
  Long<span class="token punctuation">:</span> <span class="token string">`A Fast and Flexible Static Site Generator built with
                love by spf13 and friends in Go.
                Complete documentation is available at http://hugo.spf13.com`</span><span class="token punctuation">,</span>
  Run<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">,</span> args <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// Do Stuff Here</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">Execute</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> err <span class="token operator">:=</span> rootCmd<span class="token punctuation">.</span><span class="token function">Execute</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
    os<span class="token punctuation">.</span><span class="token function">Exit</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>还可以在 <code>init()</code> 函数中定义标志和处理配置。</p><p>例如<code>cmd/root.go</code>:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">import</span> <span class="token punctuation">(</span>
  <span class="token string">&quot;fmt&quot;</span>
  <span class="token string">&quot;os&quot;</span>

  homedir <span class="token string">&quot;github.com/mitchellh/go-homedir&quot;</span>
  <span class="token string">&quot;github.com/spf13/cobra&quot;</span>
  <span class="token string">&quot;github.com/spf13/viper&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  cobra<span class="token punctuation">.</span><span class="token function">OnInitialize</span><span class="token punctuation">(</span>initConfig<span class="token punctuation">)</span>
  rootCmd<span class="token punctuation">.</span><span class="token function">PersistentFlags</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">StringVar</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>cfgFile<span class="token punctuation">,</span> <span class="token string">&quot;config&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;config file (default is $HOME/.cobra.yaml)&quot;</span><span class="token punctuation">)</span>
  rootCmd<span class="token punctuation">.</span><span class="token function">PersistentFlags</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">StringVarP</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>projectBase<span class="token punctuation">,</span> <span class="token string">&quot;projectbase&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;b&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;base project directory eg. github.com/spf13/&quot;</span><span class="token punctuation">)</span>
  rootCmd<span class="token punctuation">.</span><span class="token function">PersistentFlags</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">StringP</span><span class="token punctuation">(</span><span class="token string">&quot;author&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;a&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;YOUR NAME&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Author name for copyright attribution&quot;</span><span class="token punctuation">)</span>
  rootCmd<span class="token punctuation">.</span><span class="token function">PersistentFlags</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">StringVarP</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>userLicense<span class="token punctuation">,</span> <span class="token string">&quot;license&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;l&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Name of license for the project (can provide `licensetext` in config)&quot;</span><span class="token punctuation">)</span>
  rootCmd<span class="token punctuation">.</span><span class="token function">PersistentFlags</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Bool</span><span class="token punctuation">(</span><span class="token string">&quot;viper&quot;</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token string">&quot;Use Viper for configuration&quot;</span><span class="token punctuation">)</span>
  viper<span class="token punctuation">.</span><span class="token function">BindPFlag</span><span class="token punctuation">(</span><span class="token string">&quot;author&quot;</span><span class="token punctuation">,</span> rootCmd<span class="token punctuation">.</span><span class="token function">PersistentFlags</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Lookup</span><span class="token punctuation">(</span><span class="token string">&quot;author&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  viper<span class="token punctuation">.</span><span class="token function">BindPFlag</span><span class="token punctuation">(</span><span class="token string">&quot;projectbase&quot;</span><span class="token punctuation">,</span> rootCmd<span class="token punctuation">.</span><span class="token function">PersistentFlags</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Lookup</span><span class="token punctuation">(</span><span class="token string">&quot;projectbase&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  viper<span class="token punctuation">.</span><span class="token function">BindPFlag</span><span class="token punctuation">(</span><span class="token string">&quot;useViper&quot;</span><span class="token punctuation">,</span> rootCmd<span class="token punctuation">.</span><span class="token function">PersistentFlags</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Lookup</span><span class="token punctuation">(</span><span class="token string">&quot;viper&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  viper<span class="token punctuation">.</span><span class="token function">SetDefault</span><span class="token punctuation">(</span><span class="token string">&quot;author&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;NAME HERE &lt;EMAIL ADDRESS&gt;&quot;</span><span class="token punctuation">)</span>
  viper<span class="token punctuation">.</span><span class="token function">SetDefault</span><span class="token punctuation">(</span><span class="token string">&quot;license&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;apache&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">initConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// Don&#39;t forget to read config either from cfgFile or from home directory!</span>
  <span class="token keyword">if</span> cfgFile <span class="token operator">!=</span> <span class="token string">&quot;&quot;</span> <span class="token punctuation">{</span>
    <span class="token comment">// Use config file from the flag.</span>
    viper<span class="token punctuation">.</span><span class="token function">SetConfigFile</span><span class="token punctuation">(</span>cfgFile<span class="token punctuation">)</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token comment">// Find home directory.</span>
    home<span class="token punctuation">,</span> err <span class="token operator">:=</span> homedir<span class="token punctuation">.</span><span class="token function">Dir</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
      fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
      os<span class="token punctuation">.</span><span class="token function">Exit</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// Search config in home directory with name &quot;.cobra&quot; (without extension).</span>
    viper<span class="token punctuation">.</span><span class="token function">AddConfigPath</span><span class="token punctuation">(</span>home<span class="token punctuation">)</span>
    viper<span class="token punctuation">.</span><span class="token function">SetConfigName</span><span class="token punctuation">(</span><span class="token string">&quot;.cobra&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">if</span> err <span class="token operator">:=</span> viper<span class="token punctuation">.</span><span class="token function">ReadInConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Can&#39;t read config:&quot;</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span>
    os<span class="token punctuation">.</span><span class="token function">Exit</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="create-your-main-go" tabindex="-1"><a class="header-anchor" href="#create-your-main-go" aria-hidden="true">#</a> Create your main.go</h3><p>使用 <code>root</code> 命令，需要让 <code>main</code> 函数执行它。为了清楚起见，应该在 <code>root</code> 上运行 <code>Execute</code>，尽管可以在任何命令上调用它。</p><p>在一个 Cobra 应用里, 通常这个<code>main.go</code>文件仅仅只有一个目的：初始化 Cobra。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
  <span class="token string">&quot;{pathToYourApp}/cmd&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  cmd<span class="token punctuation">.</span><span class="token function">Execute</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="create-additional-commands" tabindex="-1"><a class="header-anchor" href="#create-additional-commands" aria-hidden="true">#</a> Create additional commands</h3><p>可以定义其他命令，通常每个命令都在 <code>cmd/</code> 目录中给出自己的文件。</p><p>如果要创建版本命令，可以创建 <code>cmd/version.go</code> 并使用以下内容填充它</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> cmd

<span class="token keyword">import</span> <span class="token punctuation">(</span>
  <span class="token string">&quot;fmt&quot;</span>

  <span class="token string">&quot;github.com/spf13/cobra&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  rootCmd<span class="token punctuation">.</span><span class="token function">AddCommand</span><span class="token punctuation">(</span>versionCmd<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> versionCmd <span class="token operator">=</span> <span class="token operator">&amp;</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">{</span>
  Use<span class="token punctuation">:</span>   <span class="token string">&quot;version&quot;</span><span class="token punctuation">,</span>
  Short<span class="token punctuation">:</span> <span class="token string">&quot;Print the version number of Hugo&quot;</span><span class="token punctuation">,</span>
  Long<span class="token punctuation">:</span>  <span class="token string">`All software has versions. This is Hugo&#39;s`</span><span class="token punctuation">,</span>
  Run<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">,</span> args <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Hugo Static Site Generator v0.9 -- HEAD&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="working-with-flags" tabindex="-1"><a class="header-anchor" href="#working-with-flags" aria-hidden="true">#</a> Working with Flags</h2><p>标志提供修饰符来控制动作命令的操作方式。</p><h3 id="assign-flags-to-a-command" tabindex="-1"><a class="header-anchor" href="#assign-flags-to-a-command" aria-hidden="true">#</a> Assign flags to a command</h3><p>由于标志是在不同的位置定义和使用的，因此我们需要在外部定义一个具有正确范围的变量来分配要使用的标志。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">var</span> Verbose <span class="token builtin">bool</span>
<span class="token keyword">var</span> Source <span class="token builtin">string</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><p>分配标志有两种不同的方法。</p><h3 id="persistent-flags" tabindex="-1"><a class="header-anchor" href="#persistent-flags" aria-hidden="true">#</a> Persistent Flags</h3><p>标志可以是“持久的”，这意味着该标志可用于它所分配的命令以及该命令下的每个命令。对于全局标志，在 <code>root</code> 上分配标志作为持久标志。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>rootCmd<span class="token punctuation">.</span><span class="token function">PersistentFlags</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">BoolVarP</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>Verbose<span class="token punctuation">,</span> <span class="token string">&quot;verbose&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;v&quot;</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token string">&quot;verbose output&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h3 id="local-flags" tabindex="-1"><a class="header-anchor" href="#local-flags" aria-hidden="true">#</a> Local Flags</h3><p>也可以在本地分配一个标志，该标志仅适用于该特定命令。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>rootCmd<span class="token punctuation">.</span><span class="token function">Flags</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">StringVarP</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>Source<span class="token punctuation">,</span> <span class="token string">&quot;source&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;s&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Source directory to read from&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h3 id="local-flag-on-parent-commands" tabindex="-1"><a class="header-anchor" href="#local-flag-on-parent-commands" aria-hidden="true">#</a> Local Flag on Parent Commands</h3><p>默认情况下，Cobra 仅解析目标命令上的本地标志，并且忽略父命令上的任何本地标志。通过启用 <code>Command.TraverseChildren</code>， Cobra 将在执行目标命令之前解析每个命令上的本地标志。</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>command <span class="token operator">:=</span> cobra<span class="token punctuation">.</span>Command<span class="token punctuation">{</span>
  Use<span class="token punctuation">:</span> <span class="token string">&quot;print [OPTIONS] [COMMANDS]&quot;</span><span class="token punctuation">,</span>
  TraverseChildren<span class="token punctuation">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="bind-flags-with-config" tabindex="-1"><a class="header-anchor" href="#bind-flags-with-config" aria-hidden="true">#</a> Bind Flags with Config</h3><p>你也可以用 <a href="https://github.com/spf13/viper" target="_blank" rel="noopener noreferrer">viper</a> 绑定标志:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">var</span> author <span class="token builtin">string</span>

<span class="token keyword">func</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  rootCmd<span class="token punctuation">.</span><span class="token function">PersistentFlags</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">StringVar</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>author<span class="token punctuation">,</span> <span class="token string">&quot;author&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;YOUR NAME&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Author name for copyright attribution&quot;</span><span class="token punctuation">)</span>
  viper<span class="token punctuation">.</span><span class="token function">BindPFlag</span><span class="token punctuation">(</span><span class="token string">&quot;author&quot;</span><span class="token punctuation">,</span> rootCmd<span class="token punctuation">.</span><span class="token function">PersistentFlags</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">Lookup</span><span class="token punctuation">(</span><span class="token string">&quot;author&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在这个例子中持久标志 <code>author</code> 被 <code>viper</code> 绑定了。 <strong>注意</strong>, 当用户没有提供 <code>--author</code> 标志时，变量 <code>author</code> 不会被设置为 config 的值。</p><p>更多参考 <a href="https://github.com/spf13/viper#working-with-flags" target="_blank" rel="noopener noreferrer">viper documentation</a>.</p><h3 id="required-flags" tabindex="-1"><a class="header-anchor" href="#required-flags" aria-hidden="true">#</a> Required flags</h3><p>标志默认是可选的。如果希望命令在未设置标志时报告错误，将其设置为 <code>required</code>:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>rootCmd<span class="token punctuation">.</span><span class="token function">Flags</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">StringVarP</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>Region<span class="token punctuation">,</span> <span class="token string">&quot;region&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;r&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;AWS region (required)&quot;</span><span class="token punctuation">)</span>
rootCmd<span class="token punctuation">.</span><span class="token function">MarkFlagRequired</span><span class="token punctuation">(</span><span class="token string">&quot;region&quot;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="positional-and-custom-arguments" tabindex="-1"><a class="header-anchor" href="#positional-and-custom-arguments" aria-hidden="true">#</a> Positional and Custom Arguments</h2><p>可以使用 <code>Command</code> 的 <code>Args</code> 字段指定位置参数的验证。</p><p>内置以下验证器:</p><ul><li><code>NoArgs</code> - 如果存在任何位置参数，该命令将报告错误。</li><li><code>ArbitraryArgs</code> - 该命令将接受任何参数。</li><li><code>OnlyValidArgs</code> - 如果有任何位置参数不在 <code>Command</code> 的 <code>ValidArgs</code> 字段中，该命令将报告错误。</li><li><code>MinimumNArgs(int)</code> - 如果没有至少 N 个位置参数，该命令将报告错误。</li><li><code>MaximumNArgs(int)</code> - 如果有多于 N 个位置参数，该命令将报告错误。</li><li><code>ExactArgs(int)</code> - 如果没有确切的 N 位置参数，该命令将报告错误。</li><li><code>ExactValidArgs(int)</code> - 如果没有正确的 N 位置参数，或者如果有任何位置参数不在 <code>Command</code> 的 <code>ValidArgs</code> 字段中，命令将报告并报错</li><li><code>RangeArgs(min, max)</code> - 如果参数的数量不在预期参数的最小和最大数量之间，则该命令将报告错误。</li></ul><p>一个设置自定义验证器的例子:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">var</span> cmd <span class="token operator">=</span> <span class="token operator">&amp;</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">{</span>
  Short<span class="token punctuation">:</span> <span class="token string">&quot;hello&quot;</span><span class="token punctuation">,</span>
  Args<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">,</span> args <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token function">len</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token number">1</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> errors<span class="token punctuation">.</span><span class="token function">New</span><span class="token punctuation">(</span><span class="token string">&quot;requires at least one arg&quot;</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> myapp<span class="token punctuation">.</span><span class="token function">IsValidColor</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token boolean">nil</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> fmt<span class="token punctuation">.</span><span class="token function">Errorf</span><span class="token punctuation">(</span><span class="token string">&quot;invalid color specified: %s&quot;</span><span class="token punctuation">,</span> args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  Run<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">,</span> args <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Hello, World!&quot;</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="example" tabindex="-1"><a class="header-anchor" href="#example" aria-hidden="true">#</a> Example</h2><p>下面的例子，我们定义了三个命令. 两个位于顶层，一个（cmdTimes）是顶级命令之一的子级。在这种情况下，<code>root</code> 不可执行，这意味着需要子命令。 这是通过不为 <code>rootCmd</code> 提供 <code>Run</code> 来实现的。</p><p>我们只为一个命令定义了一个标志。</p><p>有关标志的更多文档，访问 https://github.com/spf13/pflag</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
  <span class="token string">&quot;fmt&quot;</span>
  <span class="token string">&quot;strings&quot;</span>

  <span class="token string">&quot;github.com/spf13/cobra&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> echoTimes <span class="token builtin">int</span>

  <span class="token keyword">var</span> cmdPrint <span class="token operator">=</span> <span class="token operator">&amp;</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">{</span>
    Use<span class="token punctuation">:</span>   <span class="token string">&quot;print [string to print]&quot;</span><span class="token punctuation">,</span>
    Short<span class="token punctuation">:</span> <span class="token string">&quot;Print anything to the screen&quot;</span><span class="token punctuation">,</span>
    Long<span class="token punctuation">:</span> <span class="token string">`print is for printing anything back to the screen.For many years people have printed back to the screen.`</span><span class="token punctuation">,</span>
    Args<span class="token punctuation">:</span> cobra<span class="token punctuation">.</span><span class="token function">MinimumNArgs</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    Run<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">,</span> args <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Print: &quot;</span> <span class="token operator">+</span> strings<span class="token punctuation">.</span><span class="token function">Join</span><span class="token punctuation">(</span>args<span class="token punctuation">,</span> <span class="token string">&quot; &quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">var</span> cmdEcho <span class="token operator">=</span> <span class="token operator">&amp;</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">{</span>
    Use<span class="token punctuation">:</span>   <span class="token string">&quot;echo [string to echo]&quot;</span><span class="token punctuation">,</span>
    Short<span class="token punctuation">:</span> <span class="token string">&quot;Echo anything to the screen&quot;</span><span class="token punctuation">,</span>
    Long<span class="token punctuation">:</span> <span class="token string">`echo is for echoing anything back.Echo works a lot like print, except it has a child command.`</span><span class="token punctuation">,</span>
    Args<span class="token punctuation">:</span> cobra<span class="token punctuation">.</span><span class="token function">MinimumNArgs</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    Run<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">,</span> args <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Print: &quot;</span> <span class="token operator">+</span> strings<span class="token punctuation">.</span><span class="token function">Join</span><span class="token punctuation">(</span>args<span class="token punctuation">,</span> <span class="token string">&quot; &quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">var</span> cmdTimes <span class="token operator">=</span> <span class="token operator">&amp;</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">{</span>
    Use<span class="token punctuation">:</span>   <span class="token string">&quot;times [# times] [string to echo]&quot;</span><span class="token punctuation">,</span>
    Short<span class="token punctuation">:</span> <span class="token string">&quot;Echo anything to the screen more times&quot;</span><span class="token punctuation">,</span>
    Long<span class="token punctuation">:</span> <span class="token string">`echo things multiple times back to the user by providinga count and a string.`</span><span class="token punctuation">,</span>
    Args<span class="token punctuation">:</span> cobra<span class="token punctuation">.</span><span class="token function">MinimumNArgs</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    Run<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">,</span> args <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">for</span> i <span class="token operator">:=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> echoTimes<span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span>
        fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">&quot;Echo: &quot;</span> <span class="token operator">+</span> strings<span class="token punctuation">.</span><span class="token function">Join</span><span class="token punctuation">(</span>args<span class="token punctuation">,</span> <span class="token string">&quot; &quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span>

  cmdTimes<span class="token punctuation">.</span><span class="token function">Flags</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">IntVarP</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>echoTimes<span class="token punctuation">,</span> <span class="token string">&quot;times&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;t&quot;</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">&quot;times to echo the input&quot;</span><span class="token punctuation">)</span>

  <span class="token keyword">var</span> rootCmd <span class="token operator">=</span> <span class="token operator">&amp;</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">{</span>Use<span class="token punctuation">:</span> <span class="token string">&quot;app&quot;</span><span class="token punctuation">}</span>
  rootCmd<span class="token punctuation">.</span><span class="token function">AddCommand</span><span class="token punctuation">(</span>cmdPrint<span class="token punctuation">,</span> cmdEcho<span class="token punctuation">)</span>
  cmdEcho<span class="token punctuation">.</span><span class="token function">AddCommand</span><span class="token punctuation">(</span>cmdTimes<span class="token punctuation">)</span>
  rootCmd<span class="token punctuation">.</span><span class="token function">Execute</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>更多大型应用程序的更完整示例，请查看 <a href="http://gohugo.io/" target="_blank" rel="noopener noreferrer">Hugo</a>.</p><h2 id="help-command" tabindex="-1"><a class="header-anchor" href="#help-command" aria-hidden="true">#</a> Help Command</h2><p>当你有子命令时，Cobra 会自动为应用程序添加一个帮助命令。当用户运行 <code>app help</code> 时会调用此方法。此外，帮助还将支持所有其他命令作为输入。 比方说，你有一个叫做 <code>create</code> 的命令没有任何额外配置;当 <code>app help create</code> 被调用时，Cobra 将工作。每个命令都会自动添加 <code>--help</code> 标志。</p><h3 id="example-1" tabindex="-1"><a class="header-anchor" href="#example-1" aria-hidden="true">#</a> Example</h3><p>以下输出由Cobra自动生成。 除了定义命令和标志之外，不需要任何其他内容。</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>$ cobra <span class="token builtin class-name">help</span>

Cobra is a CLI library <span class="token keyword">for</span> Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.

Usage:
  cobra <span class="token punctuation">[</span>command<span class="token punctuation">]</span>

Available Commands:
  <span class="token function">add</span>         Add a <span class="token builtin class-name">command</span> to a Cobra Application
  <span class="token builtin class-name">help</span>        Help about any <span class="token builtin class-name">command</span>
  init        Initialize a Cobra Application

Flags:
  -a, --author string    author name <span class="token keyword">for</span> copyright attribution <span class="token punctuation">(</span>default <span class="token string">&quot;YOUR NAME&quot;</span><span class="token punctuation">)</span>
      --config string    config <span class="token function">file</span> <span class="token punctuation">(</span>default is <span class="token environment constant">$HOME</span>/.cobra.yaml<span class="token punctuation">)</span>
  -h, --help             <span class="token builtin class-name">help</span> <span class="token keyword">for</span> cobra
  -l, --license string   name of license <span class="token keyword">for</span> the project
      --viper            use Viper <span class="token keyword">for</span> configuration <span class="token punctuation">(</span>default <span class="token boolean">true</span><span class="token punctuation">)</span>

Use <span class="token string">&quot;cobra [command] --help&quot;</span> <span class="token keyword">for</span> <span class="token function">more</span> information about a command.
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>帮助像任何其他命令一样。没有特殊的逻辑或行为。事实上，如果你愿意，你可以提供自己的。</p><h3 id="defining-your-own-help" tabindex="-1"><a class="header-anchor" href="#defining-your-own-help" aria-hidden="true">#</a> Defining your own help</h3><p>可以为默认命令提供自己的帮助命令或自己的模板，使用以下函数:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>cmd<span class="token punctuation">.</span><span class="token function">SetHelpCommand</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>Command<span class="token punctuation">)</span>
cmd<span class="token punctuation">.</span><span class="token function">SetHelpFunc</span><span class="token punctuation">(</span>f <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token operator">*</span>Command<span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
cmd<span class="token punctuation">.</span><span class="token function">SetHelpTemplate</span><span class="token punctuation">(</span>s <span class="token builtin">string</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>后两者也适用于任何子命令。</p><h2 id="usage-message" tabindex="-1"><a class="header-anchor" href="#usage-message" aria-hidden="true">#</a> Usage Message</h2><p>当用户提供无效标志或无效命令时，Cobra 会通过向用户显示 <code>usage</code> 来做出响应。</p><h3 id="example-2" tabindex="-1"><a class="header-anchor" href="#example-2" aria-hidden="true">#</a> Example</h3><p>可以从上面的帮助中认识到这一点。那是因为默认帮助将用法嵌入其输出的一部分。</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>$ cobra --invalid
Error: unknown flag: --invalid
Usage:
  cobra <span class="token punctuation">[</span>command<span class="token punctuation">]</span>

Available Commands:
  <span class="token function">add</span>         Add a <span class="token builtin class-name">command</span> to a Cobra Application
  <span class="token builtin class-name">help</span>        Help about any <span class="token builtin class-name">command</span>
  init        Initialize a Cobra Application

Flags:
  -a, --author string    author name <span class="token keyword">for</span> copyright attribution <span class="token punctuation">(</span>default <span class="token string">&quot;YOUR NAME&quot;</span><span class="token punctuation">)</span>
      --config string    config <span class="token function">file</span> <span class="token punctuation">(</span>default is <span class="token environment constant">$HOME</span>/.cobra.yaml<span class="token punctuation">)</span>
  -h, --help             <span class="token builtin class-name">help</span> <span class="token keyword">for</span> cobra
  -l, --license string   name of license <span class="token keyword">for</span> the project
      --viper            use Viper <span class="token keyword">for</span> configuration <span class="token punctuation">(</span>default <span class="token boolean">true</span><span class="token punctuation">)</span>

Use <span class="token string">&quot;cobra [command] --help&quot;</span> <span class="token keyword">for</span> <span class="token function">more</span> information about a command.
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="defining-your-own-usage" tabindex="-1"><a class="header-anchor" href="#defining-your-own-usage" aria-hidden="true">#</a> Defining your own usage</h3><p>可以提供自己的 <code>usage</code> 函数或模板供 Cobra 使用。与帮助一样，函数和模板可以通过公共方法覆盖：</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>cmd<span class="token punctuation">.</span><span class="token function">SetUsageFunc</span><span class="token punctuation">(</span>f <span class="token keyword">func</span><span class="token punctuation">(</span><span class="token operator">*</span>Command<span class="token punctuation">)</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
cmd<span class="token punctuation">.</span><span class="token function">SetUsageTemplate</span><span class="token punctuation">(</span>s <span class="token builtin">string</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="version-flag" tabindex="-1"><a class="header-anchor" href="#version-flag" aria-hidden="true">#</a> Version Flag</h2><p>如果在 <code>root</code> 命令上设置了 <code>Version</code> 字段，Cobra 会添加顶级 <code>--version</code> 标志。 使用 <code>--version</code> 标志运行应用程序会使用 <code>Version</code> 模板将版本打印到 stdout。可以使用 <code>cmd.SetVersionTemplate(s string)</code> 函数自定义模板。</p><h2 id="prerun-and-postrun-hooks" tabindex="-1"><a class="header-anchor" href="#prerun-and-postrun-hooks" aria-hidden="true">#</a> PreRun and PostRun Hooks</h2><p>可以在命令的主 <code>Run</code> 函数之前或之后运行函数。<code>PersistentPreRun</code> 和 <code>PreRun</code> 函数将在 <code>Run</code> 之前执行。<code>PersistentPostRun</code> 和 <code>PostRun</code> 将在 <code>Run</code> 之后执行。如果他们没有声明自己的 <code>Persistent*Run</code> 函数将由子项继承。这些函数按以下顺序运行:</p><ul><li><code>PersistentPreRun</code></li><li><code>PreRun</code></li><li><code>Run</code></li><li><code>PostRun</code></li><li><code>PersistentPostRun</code></li></ul><p><strong>注意：父级的 <code>PreRun</code> 只会在父级命令运行时调用，子命令时不会调用的。</strong> 运行 <code>hectl detect</code> 时，子命令 <code>detect</code> 运行并不会调用父级 <code>hectl</code> 的 <code>PreRun</code> 函数。</p><p>下面是使用所有这些功能的两个命令的示例。执行子命令时，它将运行 root 命令的 <code>PersistentPreRun</code>，但不运行 root 命令的 <code>PersistentPostRun</code>:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
  <span class="token string">&quot;fmt&quot;</span>

  <span class="token string">&quot;github.com/spf13/cobra&quot;</span>
<span class="token punctuation">)</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

  <span class="token keyword">var</span> rootCmd <span class="token operator">=</span> <span class="token operator">&amp;</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">{</span>
    Use<span class="token punctuation">:</span>   <span class="token string">&quot;root [sub]&quot;</span><span class="token punctuation">,</span>
    Short<span class="token punctuation">:</span> <span class="token string">&quot;My root command&quot;</span><span class="token punctuation">,</span>
    PersistentPreRun<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">,</span> args <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;Inside rootCmd PersistentPreRun with args: %v\n&quot;</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    PreRun<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">,</span> args <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;Inside rootCmd PreRun with args: %v\n&quot;</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    Run<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">,</span> args <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;Inside rootCmd Run with args: %v\n&quot;</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    PostRun<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">,</span> args <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;Inside rootCmd PostRun with args: %v\n&quot;</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    PersistentPostRun<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">,</span> args <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;Inside rootCmd PersistentPostRun with args: %v\n&quot;</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">var</span> subCmd <span class="token operator">=</span> <span class="token operator">&amp;</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">{</span>
    Use<span class="token punctuation">:</span>   <span class="token string">&quot;sub [no options!]&quot;</span><span class="token punctuation">,</span>
    Short<span class="token punctuation">:</span> <span class="token string">&quot;My subcommand&quot;</span><span class="token punctuation">,</span>
    PreRun<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">,</span> args <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;Inside subCmd PreRun with args: %v\n&quot;</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    Run<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">,</span> args <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;Inside subCmd Run with args: %v\n&quot;</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    PostRun<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">,</span> args <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;Inside subCmd PostRun with args: %v\n&quot;</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    PersistentPostRun<span class="token punctuation">:</span> <span class="token keyword">func</span><span class="token punctuation">(</span>cmd <span class="token operator">*</span>cobra<span class="token punctuation">.</span>Command<span class="token punctuation">,</span> args <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">&quot;Inside subCmd PersistentPostRun with args: %v\n&quot;</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">}</span>

  rootCmd<span class="token punctuation">.</span><span class="token function">AddCommand</span><span class="token punctuation">(</span>subCmd<span class="token punctuation">)</span>

  rootCmd<span class="token punctuation">.</span><span class="token function">SetArgs</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
  rootCmd<span class="token punctuation">.</span><span class="token function">Execute</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
  rootCmd<span class="token punctuation">.</span><span class="token function">SetArgs</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">string</span><span class="token punctuation">{</span><span class="token string">&quot;sub&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;arg1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;arg2&quot;</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
  rootCmd<span class="token punctuation">.</span><span class="token function">Execute</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Output:</p><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>Inside rootCmd PersistentPreRun with args: []
Inside rootCmd PreRun with args: []
Inside rootCmd Run with args: []
Inside rootCmd PostRun with args: []
Inside rootCmd PersistentPostRun with args: []

Inside rootCmd PersistentPreRun with args: [arg1 arg2]
Inside subCmd PreRun with args: [arg1 arg2]
Inside subCmd Run with args: [arg1 arg2]
Inside subCmd PostRun with args: [arg1 arg2]
Inside subCmd PersistentPostRun with args: [arg1 arg2]
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="suggestions-when-unknown-command-happens" tabindex="-1"><a class="header-anchor" href="#suggestions-when-unknown-command-happens" aria-hidden="true">#</a> Suggestions when &quot;unknown command&quot; happens</h2><p>当 &quot;unknown command&quot; 错误发生时，Cobra 将自动打印建议。当错字发生时，这使得 Cobra 的行为与 <code>git</code> 命令类似。例如:</p><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>$ hugo srever
Error: unknown <span class="token builtin class-name">command</span> <span class="token string">&quot;srever&quot;</span> <span class="token keyword">for</span> <span class="token string">&quot;hugo&quot;</span>

Did you mean this?
        server

Run <span class="token string">&#39;hugo --help&#39;</span> <span class="token keyword">for</span> usage.
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>根据注册的每个子命令自动建议并使用 <a href="http://en.wikipedia.org/wiki/Levenshtein_distance" target="_blank" rel="noopener noreferrer">Levenshtein distance</a> 实现。 每个匹配最小距离为 2（忽略大小写）的注册命令将显示为建议。</p><p>如果需要在命令中禁用建议或调整字符串距离，使用:</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>command<span class="token punctuation">.</span>DisableSuggestions <span class="token operator">=</span> <span class="token boolean">true</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>or</p><div class="language-go ext-go line-numbers-mode"><pre class="language-go"><code>command<span class="token punctuation">.</span>SuggestionsMinimumDistance <span class="token operator">=</span> <span class="token number">1</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><p>你还可以使用 <code>SuggestFor</code> 属性显式设置要为其指定命令的名称。这允许建议在字符串距离方面不接近的字符串，但在你的命令集和某些你不想要别名的字符串中有意义。例如:</p><div class="language-text ext-text line-numbers-mode"><pre class="language-text"><code>$ kubectl remove
Error: unknown command &quot;remove&quot; for &quot;kubectl&quot;

Did you mean this?
        delete

Run &#39;kubectl help&#39; for usage.
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="generating-documentation-for-your-command" tabindex="-1"><a class="header-anchor" href="#generating-documentation-for-your-command" aria-hidden="true">#</a> Generating documentation for your command</h2><p>Cobra 可以按以下格式生成基于子命令，标志等的文档：</p><ul><li><a href="/posts/2023/4/docs/Cobra/docs/md_docs.html" class="">Markdown</a></li><li><a href="/posts/2023/4/docs/Cobra/docs/rest_docs.html" class="">ReStructured Text</a></li><li><a href="/posts/2023/4/docs/Cobra/docs/man_docs.html" class="">Man Page</a></li></ul><h2 id="generating-bash-completions" tabindex="-1"><a class="header-anchor" href="#generating-bash-completions" aria-hidden="true">#</a> Generating bash completions</h2><p>Cobra 可以生成 <code>bash-completion</code> 文件。如果向命令添加更多信息，这些 Completions 功能可以非常强大且灵活。 阅读更多关于 <a href="/posts/2023/4/docs/Cobra/docs/bash_completions.html" class="">Bash Completions</a>.</p><h2 id="generating-zsh-completions" tabindex="-1"><a class="header-anchor" href="#generating-zsh-completions" aria-hidden="true">#</a> Generating zsh completions</h2><p>Cobra 可以生成 <code>zsh-completion</code>。阅读更多关于 <a href="/posts/2023/4/docs/Cobra/docs/zsh_completions.html" class="">Zsh Completions</a>.</p><h1 id="contributing" tabindex="-1"><a class="header-anchor" href="#contributing" aria-hidden="true">#</a> Contributing</h1><ol><li>Fork it</li><li>Download your fork to your PC (<code>git clone https://github.com/your_username/cobra &amp;&amp; cd cobra</code>)</li><li>Create your feature branch (<code>git checkout -b my-new-feature</code>)</li><li>Make changes and add them (<code>git add .</code>)</li><li>Commit your changes (<code>git commit -m &#39;Add some feature&#39;</code>)</li><li>Push to the branch (<code>git push origin my-new-feature</code>)</li><li>Create new pull request</li></ol><h1 id="license" tabindex="-1"><a class="header-anchor" href="#license" aria-hidden="true">#</a> License</h1><p>Cobra is released under the Apache 2.0 license. See <a href="https://github.com/spf13/cobra/blob/master/LICENSE.txt" target="_blank" rel="noopener noreferrer">LICENSE.txt</a></p><!--]--><!--[--><!--]--></div><footer class="page-meta"><!----><div class="meta-item last-updated"><span class="meta-item-label">Last Updated: </span><!----></div><!----></footer><!----><!--[--><!--]--><!----></main><!--]--></div><div class="search-page" role="search"><span class="search-close"><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 448 512" width="28" height="28" fill="currentColor"><path d="M224 416c-8.188 0-16.38-3.125-22.62-9.375l-192-192c-12.5-12.5-12.5-32.75 0-45.25s32.75-12.5 45.25 0L224 338.8l169.4-169.4c12.5-12.5 32.75-12.5 45.25 0s12.5 32.75 0 45.25l-192 192C240.4 412.9 232.2 416 224 416z"></path></svg></span><div class="gungnir-search-box"><input placeholder="$ grep ..." autocomplete="off" spellcheck="false" value><!----></div></div><div class="menu-btn-container"><div class="menu-btn-wrapper"><div class="menu-btn"><div style="" class="menu-btn-icon"><span></span><span></span><span></span></div><div style="display:none;" class="menu-text">0</div><svg class="menu-progress"><circle class="menu-border" cx="50%" cy="50%" r="48%" style="stroke-dasharray:0% 314.15926%;"></circle></svg></div><div class="menu-btn-child-wrapper"><div class="toggle-dark-button menu-btn-child" title="toggle dark mode"><svg class="ov-icon" style="font-size:1.2em;display:none;" aria-hidden="true" width="19.2" height="19.2" viewbox="-43.52 -43.52 599.04 599.04" fill="currentColor"><path d="M256 160c-52.9 0-96 43.1-96 96s43.1 96 96 96 96-43.1 96-96-43.1-96-96-96zm246.4 80.5l-94.7-47.3 33.5-100.4c4.5-13.6-8.4-26.5-21.9-21.9l-100.4 33.5-47.4-94.8c-6.4-12.8-24.6-12.8-31 0l-47.3 94.7L92.7 70.8c-13.6-4.5-26.5 8.4-21.9 21.9l33.5 100.4-94.7 47.4c-12.8 6.4-12.8 24.6 0 31l94.7 47.3-33.5 100.5c-4.5 13.6 8.4 26.5 21.9 21.9l100.4-33.5 47.3 94.7c6.4 12.8 24.6 12.8 31 0l47.3-94.7 100.4 33.5c13.6 4.5 26.5-8.4 21.9-21.9l-33.5-100.4 94.7-47.3c13-6.5 13-24.7.2-31.1zm-155.9 106c-49.9 49.9-131.1 49.9-181 0-49.9-49.9-49.9-131.1 0-181 49.9-49.9 131.1-49.9 181 0 49.9 49.9 49.9 131.1 0 181z"/></svg><svg class="ov-icon" style="font-size:1.2em;display:none;" aria-hidden="true" width="19.2" height="19.2" viewbox="-43.52 -43.52 599.04 599.04" fill="currentColor"><path d="M283.211 512c78.962 0 151.079-35.925 198.857-94.792 7.068-8.708-.639-21.43-11.562-19.35-124.203 23.654-238.262-71.576-238.262-196.954 0-72.222 38.662-138.635 101.498-174.394 9.686-5.512 7.25-20.197-3.756-22.23A258.156 258.156 0 00283.211 0c-141.309 0-256 114.511-256 256 0 141.309 114.511 256 256 256z"/></svg><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-43.52 -43.52 599.04 599.04" fill="currentColor"><path d="M224 96l16-32 32-16-32-16-16-32-16 32-32 16 32 16 16 32zM80 160l26.66-53.33L160 80l-53.34-26.67L80 0 53.34 53.33 0 80l53.34 26.67L80 160zm352 128l-26.66 53.33L352 368l53.34 26.67L432 448l26.66-53.33L512 368l-53.34-26.67L432 288zm70.62-193.77L417.77 9.38C411.53 3.12 403.34 0 395.15 0c-8.19 0-16.38 3.12-22.63 9.38L9.38 372.52c-12.5 12.5-12.5 32.76 0 45.25l84.85 84.85c6.25 6.25 14.44 9.37 22.62 9.37 8.19 0 16.38-3.12 22.63-9.37l363.14-363.15c12.5-12.48 12.5-32.75 0-45.24zM359.45 203.46l-50.91-50.91 86.6-86.6 50.91 50.91-86.6 86.6z"/></svg></div><div class="menu-btn-child"><svg class="ov-icon" style="font-size:1.7999999999999998em;" aria-hidden="true" width="28.799999999999997" height="28.799999999999997" viewbox="-75.52 -43.52 599.04 599.04" fill="currentColor"><path d="M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z"/></svg></div><div class="menu-btn-child"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-75.52 -43.52 599.04 599.04" fill="currentColor"><path d="M240.971 130.524l194.343 194.343c9.373 9.373 9.373 24.569 0 33.941l-22.667 22.667c-9.357 9.357-24.522 9.375-33.901.04L224 227.495 69.255 381.516c-9.379 9.335-24.544 9.317-33.901-.04l-22.667-22.667c-9.373-9.373-9.373-24.569 0-33.941L207.03 130.525c9.372-9.373 24.568-9.373 33.941-.001z"/></svg></div><!----><div class="toggle-sidebar-button menu-btn-child menu-btn-sidebar" title="toggle sidebar" aria-expanded="false" role="button" tabindex="0"><svg class="ov-icon" style="font-size:1.2em;" aria-hidden="true" width="19.2" height="19.2" viewbox="-1.6 -1.6 19.2 19.2" fill="currentColor"><path d="M14 2a1 1 0 011 1v10a1 1 0 01-1 1H2a1 1 0 01-1-1V3a1 1 0 011-1h12zM2 1a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V3a2 2 0 00-2-2H2z"/><path d="M3 4a1 1 0 011-1h2a1 1 0 011 1v8a1 1 0 01-1 1H4a1 1 0 01-1-1V4z"/></svg></div></div></div></div><!----></div><!--]--></div>
    <script type="module" src="/assets/app.80e5dc69.js" defer></script>
  </body>
</html>
